package app.imttf.internal.backing.admin.role;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;

import javax.faces.model.SelectItem;

import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.model.binding.DCIteratorBinding;
import oracle.adf.view.rich.component.rich.data.RichTable;

import oracle.binding.AttributeBinding;
import oracle.binding.BindingContainer;
import oracle.binding.OperationBinding;

import oracle.jbo.Row;

public class RoleCabangBackingBean implements Serializable {

    // Delete operations
    private RichTable childTable;

    // Shuttle operations
    List selectedRoles;
    List allRoles;
    Boolean refreshSelectedList = false;

    public RoleCabangBackingBean() {
    }

    public BindingContainer getBindings() {
        return BindingContext.getCurrent().getCurrentBindingsEntry();
    }

    public String doCommit() {
        BindingContainer bindings = getBindings();
        OperationBinding operationBinding =
            bindings.getOperationBinding("Commit");
        Object result = operationBinding.execute();
        if (!operationBinding.getErrors().isEmpty()) {
            return null;
        }
        return null;
    }

    public Object getCurrentRoleId() {
        BindingContext bctx = BindingContext.getCurrent();
        DCBindingContainer bindings =
            (DCBindingContainer)bctx.getCurrentBindingsEntry();
        AttributeBinding attr = (AttributeBinding)bindings.get("Id");
        //System.out.println(attr.getInputValue());
        Object roleId = attr.getInputValue();
        return roleId;
    }

    public String processShuttle() {
        BindingContext bctx = BindingContext.getCurrent();
        DCBindingContainer binding =
            (DCBindingContainer)bctx.getCurrentBindingsEntry();
        DCIteratorBinding iter =
            (DCIteratorBinding)binding.get("RoleCabangViewObjIterator"); 

        //Removing all rows
        for (Row r : iter.getAllRowsInRange()) {
            r.remove();
        } 

         if (this.getSelectedRoles().size() > 0) {
            for (int i = 0; i < getSelectedRoles().size(); i++) {

                Row row = iter.getRowSetIterator().createRow();
                
                System.out.println(i);

                row.setNewRowState(Row.STATUS_INITIALIZED);
                row.setAttribute("RoleId", this.getCurrentRoleId());
                row.setAttribute("BranchCode", getSelectedRoles().get(i));
                System.out.println(this.getCurrentRoleId() + "-" + getSelectedRoles().get(i));

                iter.getRowSetIterator().insertRow(row);
                iter.setCurrentRowWithKey(row.getKey().toStringFormat(true));
            } 
        }
        String ok = doCommit();
        return null; 
    }

    public void setSelectedRoles(List selectedRoles) {
        this.selectedRoles = selectedRoles;
    }

    public List getSelectedRoles() {

        if (selectedRoles == null || refreshSelectedList) {
            selectedRoles =
                    attributeListForIterator("RoleCabangViewObjIterator",
                                             "BranchCode");
        }
        return selectedRoles;
    }

    public void setAllRoles(List allRoles) {
        this.allRoles = allRoles;
    }

    public List getAllRoles() {
        if (allRoles == null) {
            allRoles =
                    selectItemsForIterator("InfoCabangViewObj1Iterator", "BranchCode", "BranchName");
        }
        return allRoles;
    }

    public static List<SelectItem> selectItemsForIterator(String iteratorName,
                                                          String valueAttrName,
                                                          String displayAttrName) {
        BindingContext bc = BindingContext.getCurrent();
        DCBindingContainer binding =
            (DCBindingContainer)bc.getCurrentBindingsEntry();
        DCIteratorBinding iter = binding.findIteratorBinding(iteratorName);
        List<SelectItem> selectItems = new ArrayList<SelectItem>();
        for (Row r : iter.getAllRowsInRange()) {
            selectItems.add(new SelectItem(r.getAttribute(valueAttrName),
                                           (String)r.getAttribute(displayAttrName)));
        }
        return selectItems;
    }

    // Shuttle operations

    public static List attributeListForIterator(String iteratorName,
                                                String valueAttrName) {
        BindingContext bc = BindingContext.getCurrent();
        DCBindingContainer binding =
            (DCBindingContainer)bc.getCurrentBindingsEntry();
        DCIteratorBinding iter = binding.findIteratorBinding(iteratorName);
        List attributeList = new ArrayList();
        for (Row r : iter.getAllRowsInRange()) {
            attributeList.add(r.getAttribute(valueAttrName));
        }
        return attributeList;
    }
}
